/**************************************************************************
 * @doc PIP/ISO
 * @module isoload.c - Source code for the functions which load the fields
 * in the field table.The functions are called by the inLoadTransData|  
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   Monique Brown.
 * <nl>Notes       :
 *
 * @head3 ISO Load Functions |
 * @index | PIP
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 * 
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form 
 * or by any means whether electronic, mechanical, magnetic, optical, 
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
$Log: /SoftPay/SPK2SIM/Source/PIP/isoload.C $
 ********************************************************************************/
#include <project.h>
#define ISOLOAD_C

#include <aclconio.h>
#include <stdio.h>
#include <stdlib.h>
#include <acldev.h>
#include <aclstr.h>
#include <aclutil.h>
#include <ascii.h>
#include <amexhost.h>
#include <svctxo.h>
#include <iso8583.h>
#include <message.h>
#include <xmodem.h>
#include <errno.h>
#include <string.h>

#ifdef PIP_DEFINED

#ifdef DEBUG_VSPIP
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <tran.h>
#include <pipmsg.h>
#include <msgfuncs.h>
#include <date.h>
#include <bdt.h>
#include <tct.h>
#include <spdebug.h>
#include <isoutl.h>
#include <isodefs.h>
#include <isoload.h>
#include <ifldtbl.h>
#include <trt.h>
#include <tip.h>
#include <codesec.h>

ISOLOAD srDefaultLoadDataFuncs[] = {
    2,
    vdLoadField02,
    4,
    vdLoadField04,
    11,
    vdLoadField11,
    12,
    vdLoadField12,
    13,
    vdLoadField13,
    14,
    vdLoadField14,
    22,
    vdLoadField22,
    25,
    vdLoadField25,
    35,
    vdLoadField35,
    38,
    vdLoadField38,
    45,
    vdLoadField45,
    54,
    vdLoadField54,

};

ISOLOAD* psrLoadDataFuncs = srDefaultLoadDataFuncs;

void vdOverLoadLoadDataFuncs(ISOLOAD srNewLoadDataFuncs[]) {
    psrLoadDataFuncs = srNewLoadDataFuncs;
}


/********************************************************************************************* 
* @func void | vdDummy |
* Dummy Load function
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdDummy(TRANSACTION_OBJECT* pobTran) {
    return;
}
/********************************************************************************************* 
* @func void | vdLoadField02 |
* Load the Card Account Number - ISO Field # 2
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField02(TRANSACTION_OBJECT* pobTran) {
	
    strlcpy((char *) srISO.pszField02, pobTran->srBRec.szPAN, PAN_SIZE);
    return;
}

/********************************************************************************************* 
* @func void | vdLoadField04 |
* Load the Transaction Amount - ISO Field # 4 
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField04(TRANSACTION_OBJECT* pobTran) {
    int inTranCode;
    long lnTransactionAmt;
    int inRepeat;
    int inPercentOver;
    char szScratch[25];
    char szAmt[AMT_SIZE + 1];

    memset(szAmt, 0x00, sizeof(szAmt));

    if (fGetOffline(pobTran))
        inPercentOver = 0;
    else
        inPercentOver = inGetPerOver();

    inTranCode = pobTran->srBRec.inCode;
    do {
        inRepeat = VS_FALSE;
        if (pobTran->srBKey.fVoided) {
            vdGetMessageFromThisFile(PIP_MESSAGE_FILE, TWENTY_ZEROS_MSG, szScratch);
            szScratch[12] = '\0';
            strcpy((char *) srISO.pszField04, szScratch);
        } else
            switch (inTranCode) {
            case SALE:
                lnTransactionAmt = pobTran->srBKey.lnTotalOfTransactionAmount;
                if (fComputeOverage(pobTran)) {
                    inComputePercent((long) inPercentOver, szAmt, lnTransactionAmt, WHOLE_PERCENT_FORMAT);
                    lnTransactionAmt += atol(szAmt);
                }
                sprintf((char *) srISO.pszField04, SUBSTITUTE_LD, lnTransactionAmt);
                break;
            case BATCH_UPLOAD:
                inTranCode = pobTran->srBRec.inOrgCode;
                inPercentOver = 0;
                inRepeat = VS_TRUE;
                break;    
            case CHECK_TRAN:
                sprintf((char *) srISO.pszField04, SUBSTITUTE_LD, pobTran->srBRec.lnBaseTransactionAmount);
                break; 

            default:
                sprintf((char *) srISO.pszField04, SUBSTITUTE_LD, pobTran->srBKey.lnTotalOfTransactionAmount);
            }
    } while (inRepeat);
}
/********************************************************************************************* 
* @func void | vdLoadField11 |
* Load The STAN - ISO Field # 11
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField11(TRANSACTION_OBJECT* pobTran) {
    srISO.lnField11 = pobTran->srBRec.lnSTAN;
    long2str((char *) srISO.pszField11, pobTran->srBRec.lnSTAN);
    if ((pobTran->srTRTRec.TransactionCode == TIP_ADJUST) || (pobTran->srTRTRec.TransactionCode == ADJUST) || (pobTran->srTRTRec.TransactionCode == SALE_ADJST))
        pobTran->srBKey.lnOrgSTAN = pobTran->srBRec.lnSTAN;
}
/********************************************************************************************* 
* @func void | vdLoadField12 |
* Load Transaction Time - ISO Field # 12
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField12(TRANSACTION_OBJECT* pobTran) {
    strlcpy((char *) srISO.pszField12, pobTran->srBRec.szTime, DATE_SIZE);
}

/********************************************************************************************* 
* @func void | vdLoadField13 |
* Load Transaction Date - ISO Field # 13
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField13(TRANSACTION_OBJECT* pobTran) {
    strlcpy((char *) srISO.pszField13, pobTran->srBRec.szDate, EXP_DATE_SIZE);
}
/********************************************************************************************* 
* @func void | vdLoadField14 |
* Load Expiration Date - ISO Field # 14
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField14(TRANSACTION_OBJECT* pobTran) {
    char szDate[DATE_SIZE + 1];
    char szTempYear[2];

    strlcpy(szDate, pobTran->srBRec.szExpDate, EXP_DATE_SIZE);
    strlcpy(szTempYear, szDate, 2);
    strlcpy(szDate, szDate + 2, 2);
    strlcpy(szDate + 2, szTempYear, 2);
    strlcpy((char *) srISO.pszField14, szDate, EXP_DATE_SIZE);
}       
/********************************************************************************************* 
* @func void | vdLoadField22 |
* Load the POS Entry Mode - ISO Field # 22
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField22(TRANSACTION_OBJECT* pobTran) {
    if (pobTran->srBRec.inTranCardType == DEBIT_CARD) {
        if (pobTran->srBRec.fManual == VS_TRUE)
            strlcpy((char *) srISO.pszField22, MANUAL_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);
        else
            strlcpy((char *) srISO.pszField22, SWIPE_POS_DEBIT_ENTRY_CODE, POS_EM_SIZE);
    } else {
        if (pobTran->srBRec.fManual == VS_TRUE)
            strlcpy((char *) srISO.pszField22, MANUAL_POS_ENTRY_CODE, POS_EM_SIZE);
        else
            strlcpy((char *) srISO.pszField22, SWIPE_POS_ENTRY_CODE, POS_EM_SIZE);
    }
}
/********************************************************************************************* 
* @func void | vdLoadField25 |
* Load the POS Condition Code  - ISO Field # 25
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField25(TRANSACTION_OBJECT* pobTran) {
    strlcpy((char *) srISO.pszField25, "00", POS_CON_SIZE);
    return;
}
/********************************************************************************************* 
* @func void | vdLoadField35 |
* Load Track 2 data - ISO Field # 35
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField35(TRANSACTION_OBJECT* pobTran) {
    memcpy((char *) srISO.pszField35, pobTran->srBRec.stT2Data, TRACK2_SIZE);
}
/********************************************************************************************* 
* @func void | vdLoadField38 |
* Load the Response Auth Code - ISO Field # 38, This is done for Offline Refund 
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField38(TRANSACTION_OBJECT* pobTran) {
    int inTranCode;

    inTranCode = pobTran->srBRec.inCode;

    //   if (inTranCode == REFUND_OFFLINE)
    if (inTranCode == REFUND && fGetOffline(pobTran)) {
        MEMCLR(pobTran->srBRec.szAuthCode, AUTH_CODE_SIZE);
        memset(pobTran->srBRec.szAuthCode, '0', AUTH_CODE_SIZE);
        memset((char *) srISO.pszField38, '0', AUTH_CODE_SIZE);
    } else {
        strlcpy((char *) srISO.pszField38, pobTran->srBRec.szAuthCode, AUTH_CODE_SIZE);
        pad((char *) srISO.pszField38, (char *) srISO.pszField38, SPACE, AUTH_CODE_SIZE, LEFT);
    }
}
/********************************************************************************************* 
* @func void | vdLoadField45 |
* Load Track 1 data - ISO Field # 45
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField45(TRANSACTION_OBJECT* pobTran) {
    memcpy(srISO.pszField45, pobTran->srBRec.stT1Data, TRACK1_SIZE);
}

/********************************************************************************************* 
* @func void | vdLoadField54 |
* Load the additional amount field - ISO Field # 54 
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdLoadField54(TRANSACTION_OBJECT* pobTran) {
    char szScratch[25];
    int fIncludeTipField = VS_FALSE;


    if (pobTran->srBKey.fVoided) {
        vdGetMessageFromThisFile(PIP_MESSAGE_FILE, TWENTY_ZEROS_MSG, szScratch);
        szScratch[3] = '\0';
        strcpy((char *) srISO.pszField54, szScratch);
    } else {
        if ((pobTran->fTipAdjust) && !pobTran->srBKey.fUploaded && pobTran->srBRec.fTipEntered)
            fIncludeTipField = VS_TRUE;
        sprintf((char *) srISO.pszField54, SUBSTITUTE_LD, pobTran->srBRec.lnTipAmount);
    }

    if (atol((char *) srISO.pszField54) == 0L && !fIncludeTipField)
        map_reset(stOptionalMap, 54);
    else
        pad((char *) srISO.pszField54, (char *) srISO.pszField54, '0', ISO_AMT_SIZE, RIGHT);
}

int ISOLOAD_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}


#endif /* PIP_DEFINED */

#undef ISOLOAD_C

